﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;

namespace P03
{
    public class A05 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P03A05 P = (P03A05)handler.SerialApi.Deserialize(typeof(P03A05), parameter, CR.SerialType);

            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@Serial", SqlDbType.NChar, 50);
                sps[0].Value = P.Serial;

                String sql = "Select A.FNumber CardNumber,A.FText2 BillID,A.FComboBox FType,B.FText BaseName,B.FText1 IpAdd,B.FText2 FUser,B.FText3 Pswd,B.FNumber Account From KEW_ICCardEntry A LEFT JOIN KEW_DataBaseEntry B ON A.FBase=B.FID Where A.FText=@Serial";
                Recordset rs = handler.Data.ExeBase(sql, sps);

                if (rs.Enable)
                {
                    BaseCardServer bc = new BaseCardServer();

                    if (rs == null || rs.Data.Tables.Count <= 0 || rs.Data.Tables[0].Rows.Count <= 0)
                    {
                        CR.Result = EnumResult.Error;
                        CR.Message = "磁卡信息不存在";
                        return;
                    }

                    foreach (DataTable dt in rs.Data.Tables)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            bc.CardNumber = dr["CardNumber"].ToString();
                            bc.BillID = dr["BillID"].ToString();
                            bc.FType = dr["FType"].ToString();
                            bc.BaseName = dr["BaseName"].ToString();
                            bc.IpAdd = dr["IpAdd"].ToString();
                            bc.FUser = dr["FUser"].ToString();
                            bc.Pswd = dr["Pswd"].ToString();
                            bc.Account = dr["Account"].ToString();
                        }
                    }

                    CR.Result = EnumResult.Normal;
                    CR.Content = handler.SerialApi.Serializer(typeof(BaseCardServer), bc, CR.SerialType);
                    handler.Message(MessageType.Note, String.Format("操作员{0}对单据{1}进行了磁卡信息查询", user.UserName, P.Serial));
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "数据库错误";
                }
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = ex.Message;
                handler.Message(MessageType.Error, String.Format("发生错误:{0}", ex.Message));
            }
        }
    }

    public class P03A05
    {
        public string Serial;
    }

    public class BaseCardServer
    {
        public string CardNumber;
        public string BillID;
        public string FType;
        public string BaseName;
        public string IpAdd;
        public string FUser;
        public string Pswd;
        public string Account;
    }
}
